package com.ze.common.Validator;

import com.ze.common.annotation.NewGZ;
import com.ze.common.utils.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author zzg
 * @date 2024-06-20 17:37
 * 自定义属性匹配规则验证器
 */
public class NewGZValidator implements ConstraintValidator<NewGZ, String> {

    private String rule;

    @Override
    public void initialize(NewGZ constraintAnnotation) {
        this.rule = constraintAnnotation.value();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        switch (rule) {
            case "Phone":
                return Pattern.matches("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$", value);
            case "Email":
                return Pattern.matches("^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", value);
            case "PasswordZS6-16":
                return Pattern.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$", value);
            default:
                return true;
        }
    }
}
